<?php
include 'securities.php';
class Manage_users extends Securities {
	function __construct() {
		parent::__construct('manage_users');
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}

	//Function use to list all user that can access to our backend system
	public function index() {

		$data['title'] = $this->lang->line('title_list_users');
		$data['controller_name'] = strtolower(get_class());
		//Query select user data and pass value to table helper and return to view.(You can see the table helper in folder helper in side application folder)
		$data['manage_datatable'] = get_users_manage_table($this -> mod_globals -> select_all(PREFIX_TABLE . 'users'), $this -> uri -> segment(1));
		$this -> load -> view(B_MASTER . 'index', $data);
	}
	
	//Function use to create user that can be access to our backend system
	public function insert_user() {
		$module_id = $this -> uri -> segment(1);

		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}
		//General validation user input with dynamic lanuage
		$data['title'] = $this->lang->line('title_new_user');
		$this -> form_validation -> set_rules('username', 'lang:form_label_username', 'required');
		$this -> form_validation -> set_rules('email', 'lang:form_label_email', 'required|valid_email');
		$this -> form_validation -> set_rules('first_name', 'lang:form_label_first_name', 'required');
		$this -> form_validation -> set_rules('last_name', 'lang:form_label_last_name', 'required');

		$this -> form_validation -> set_rules('password', 'lang:form_label_password', 'trim|min_length[4]|max_length[25|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'lang:form_label_confirm_password', 'trim|min_length[4]|max_length[25]');

		if ($this -> form_validation -> run() === FALSE) {
			$data['dropdown_user_type'] = get_dropdown_list_user_type($this -> mod_globals -> select_all(PREFIX_TABLE . 'user_groups'));
			//$this->load->view('welcome_message',$data);
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {
			// Create users table
			$data_create_users = array('use_user_group_id' => $this -> input -> post('user_group'), 'username' => $this -> input -> post('username'), 'first_name' => $this -> input -> post('first_name'), 'last_name' => $this -> input -> post('last_name'), 'email' => $this -> input -> post('email'), 'password' => md5($this -> input -> post('password')), 'status' => $this -> input -> post('status'), 'created_date' => null);

			$is_saved = $this -> mod_globals -> insert(PREFIX_TABLE . "users", $data_create_users);
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'User  was created success.');
				redirect('manage-users');
			} else {
				$this -> session -> set_flashdata('error', 'User  was created fail.');
				redirect('manage-users');
			}

		}
	}

	//Function use to update or modify user information that access our backend website
	public function update_user() {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$data['title'] = $this->lang->line('title_update_user');
		$user_id = $this -> uri -> segment(3);
		//General validation user input with dynamic lanuage
		$this -> form_validation -> set_rules('username', 'lang:form_label_username', 'required');
		$this -> form_validation -> set_rules('email', 'lang:form_label_email', 'required|valid_email');
		$this -> form_validation -> set_rules('first_name', 'lang:form_label_first_name', 'required');
		$this -> form_validation -> set_rules('last_name', 'lang:form_label_last_name', 'required');

		$this -> form_validation -> set_rules('password', 'lang:form_label_password', 'trim|min_length[4]|max_length[25|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'lang:form_label_confirm_password', 'trim|min_length[4]|max_length[25]');

		if ($this -> form_validation -> run() === FALSE) {

			$data['user_data'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "users", array('user_id' => $user_id));
			$data['dropdown_user_type'] = get_dropdown_list_user_type_select($this -> mod_globals -> select_all(PREFIX_TABLE . 'user_groups'), $data['user_data'] -> row() -> use_user_group_id);
			//$this->load->view('welcome_message',$data);
			$this -> load -> view(B_MASTER . 'index', $data);

		} else {

			if ($this -> input -> post('password') == "") {

				$password = $this -> mod_globals -> select_string(PREFIX_TABLE . "users", "password", array("user_id" => $user_id));
			} else {

				$password = md5($this -> input -> post('password'));
			}
			// Create users table
			$data_update_users = array('use_user_group_id' => $this -> input -> post('user_group'), 'username' => $this -> input -> post('username'), 'first_name' => $this -> input -> post('first_name'), 'last_name' => $this -> input -> post('last_name'), 'email' => $this -> input -> post('email'), 'password' => $password, 'status' => $this -> input -> post('status'));

			$is_updated = $this -> mod_globals -> update(PREFIX_TABLE . "users", $data_update_users, array('user_id' => $user_id));
			if ($is_updated) {
				$this -> session -> set_flashdata('success', 'User data was updated success.');
				redirect('manage-users');
			} else {
				$this -> session -> set_flashdata('error', 'User data was updated fail.');
				redirect('manage-users');
			}

		}
	}
	//Function used to delete user access the backend system
	public function delete_user($user_id="") {
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$whe_delete_user = array('user_id' => $user_id);
		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . 'users', $whe_delete_user);
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'User data was deleted success.');
			redirect('manage-users');
		} else {
			$this -> session -> set_flashdata('error', 'User data was deleted fail.');
			redirect('manage-users');
		}
	}

}
